<template>
	<view class="index">
		<u-sticky>
			<Navigation backType="city" background="#FABB3C" rightType="news" />
			<view class="pv-10 search ph-24" @click="searchTap" style="

			z-index: 111;
			width: 100%;">
				<u-search placeholder="请输入关键词" height="30" bgColor="#FFFFFF" searchIconColor="#999"
					placeholderColor="#999" clearabled :showAction="false" disabled>
				</u-search>
				<view class="w-100 search-btn"><u-button text="搜索" color="#FABB3C" shape="circle"
						@click.stop="toSearch"></u-button></view>
			</view>
		</u-sticky>

		<!-- <view style="margin-top:60rpx"></view> -->
		<u-swiper :list="bannerLists" radius="0" height="200" keyName="picUrl"></u-swiper>
		<view class="content ph-20">
			<!-- <view @click="jumpUrl">测试跳转</view> -->
			<Menu />
			<SpecializedDiseases v-if="toMeun('首页-专科专病')=='1'"/>
			<Doctor ref="doctor" v-if="toMeun('首页-名医在线')=='1'" />
			<Insure v-if="toMeun('首页-健康保险')=='1'" />
			<Hot :seckillList="seckillList" v-if="toMeun('首页-热门活动')=='1'" />
			<Recommend v-if="toMeun('首页-相关推荐')=='1'" />
			<Cupon />
		</view>
		<!-- tab -->
		<!-- <Tabbar tab="0" /> -->
	</view>
</template>
<script setup lang="ts">
	import { ref } from 'vue';
	import { useAuthStore } from '@/store/modules/auth';
	import { onShow, onLoad } from '@dcloudio/uni-app';
	import Menu from './Menu.vue';
	import SpecializedDiseases from './SpecializedDiseases.vue';
	import Doctor from './Doctor.vue';
	import Insure from './Insure.vue';
	import Hot from './Hot.vue';
	import Recommend from './Recommend.vue';
	import Cupon from './Cupon.vue';
	import { bannerList, doctorPage } from '@/services/api/index/index';
	import { spuPage } from '@/services/api/shop/index';
	import { dictDataType } from '@/services/api/dict';
	const authStore = useAuthStore();
	const address = ref<string | null>(null);
	const bannerLists = ref([]);
	const doctor = ref([]);
	const seckillList = ref([]);
	const meunList = ref([]);
	onShow(() => {
		//城市
		const cityName = ref<string | undefined>('');
		cityName.value = authStore.getCity;
		if (!cityName.value) {
			getCity();
		}
		getBannerLists();
		getSeckillList();
		getDoctor();
	});
	onLoad(() => {
		
		getCity();
		getDict()
	})
	function toMeun(name) {
		let isShow = 1
		for (var i in meunList.value) {
			if (name == meunList.value[i].label) {
				isShow = meunList.value[i].remark
			}
		}
		return isShow
	}
	function jumpUrl() {
		uni.navigateTo({ url: "/TUICallKit/pages/call" });
	}

	async function getDict() {
		const res = await dictDataType({ type: 'user_model' })
		meunList.value = res.data
	}
	//获取banner
	async function getBannerLists() {
		const { data } = await bannerList({ positionName: '首页' });
		bannerLists.value = data;
	}
	//获取活动
	async function getSeckillList() {
		spuPage({ pageNo: 1, pageSize: 10, type: 1, sortAsc: true }).then((res : any) => {
			res.data.list.forEach((item : any, index : number) => {
				let list = item.picUrl.split(',')
				if (list.length > 0) {
					item.picUrl = list[0]
				}
			});
			seckillList.value = res.data.list;
		});
	}
	//获取医生列表
	async function getDoctor() {
		const { data } = await doctorPage({
			pageNo: 1,
			pageSize: 10,
		});
		doctor.value = data.list;
	}
	//搜索
	const searchTap = () => {
		uni.navigateTo({
			url: '/pages_index/search',
		});
	};

	function getCity() {
		uni.getLocation({
			type: 'wgs84',
			geocode: true,
			success: function (res) {
				console.log(res);
				console.log('当前位置的纬度：' + res.latitude);
				let location = res.latitude + ',' + res.longitude;
				uni.request({
					url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=location',
					data: {
						location: location,
						key: 'X3HBZ-6K2CX-JRP4Z-ZH4VQ-K2YNZ-IMFFD', //自己申请的Key
					},
					method: 'GET',
					success(res) {
						address.value = res.data.result.address
						authStore.setCity(res.data.result.formatted_addresses.recommend)
						// console.log(res, '解析成功返回的参数');
					},
				});
			},
			fail: function (res) {
				console.log(res);
			},
		});
	}
	function loadCity(longitude : string, latitude : string) {
		// uni.request({
		//     header: {
		//         'Content-Type': 'application/text',
		//     },
		//     //注意:这里的key值需要高德地图的 web服务生成的key  只有web服务才有逆地理编码
		//     url:
		//         'https://restapi.amap.com/v3/geocode/regeo?output=JSON&location=' +
		//         longitude +
		//         ',' +
		//         latitude +
		//         '&key=' +
		//         _this.key +
		//         '&radius=1000&extensions=all',
		//     success(res) {
		//         if (res.statusCode === 200) {
		//             console.log('获取中文街道地理位置成功',res;
		//         } else {
		//             console.log('获取信息失败，请重试！');
		//         }
		//     },
		// });
	}
</script>
<style lang="scss" scoped>
	.index {
		background: #f5f5f5;

		.content {
			position: relative;
			top: -100rpx;
		}

		.search {
			position: relative;
			background: #fabb3c;

			.search-btn {
				position: absolute;
				right: 30rpx;
				top: 18rpx;
				z-index: 11;
			}

			::v-deep .u-button__text {
				font-size: 22rpx !important;
				font-weight: 400 !important;
			}

			::v-deep .u-button {
				height: 48rpx !important;
			}
		}
	}
</style>